package cclub.demo.service;

import cclub.demo.constant.cacheKey;
import cclub.demo.mapper.UserMapper;
import cclub.demo.pojo.DO.userDO;
import cclub.demo.service.intf.userService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cclub.inteface.service.common.redisTools;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class userServiceImpl extends ServiceImpl<UserMapper, userDO> implements userService{

    @Reference(version = "1.0.0")
    private redisTools redisTools;

    @Override
    public userDO registerUser(String mobile) {
        userDO  user = new userDO();
        user.setMobile(mobile);
        QueryWrapper<userDO> queryWrapper = new QueryWrapper<>();
        userDO userDO = baseMapper.selectOne(queryWrapper);
        if(userDO == null){
            user.setId(baseMapper.insert(user));
            return user;
        }
        return userDO;
    }

    @Override
    public userDO getUserInfo(int userId) {
        userDO userInfo = (userDO) redisTools.getCache(cacheKey.USER_INFO+userId);
        if(userInfo == null){
            userInfo = baseMapper.selectById(userId);
            redisTools.setCache(cacheKey.USER_INFO+userInfo.getId(),userInfo,cacheKey.USER_LOGIN_TIME);
        }
        return userInfo;
    }
}
